package com.yunda.nebula.gateway.filter;

import com.yunda.nebula.gateway.filter.config.StatConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

/**
 * \* User: wei.zhang
 * \* Date: 2020/9/18 15:02
 * \* Description:
 *
 * @author wei.zhang
 * \
 */
@Service
@Slf4j
public class StatGateWayFilterFactory extends AbstractGatewayFilterFactory<StatConfig> {

    public StatGateWayFilterFactory(){
        super(StatConfig.class);
    }

    @Override
    public GatewayFilter apply(StatConfig config) {
        return ((exchange, chain) -> {
//            boolean root = "root".equals(config.getUrl());
//            if (root){
//                log.info("GatewayFilter root");
//            }
//            else {
//                log.info("GatewayFilter customer");
//            }
            log.info(config.getId());
            // 在then方法里的，相当于aop中的后置通知
            return chain.filter(exchange).then(Mono.fromRunnable(()->{
                // do something
            }));
        });
    }

    @Override
    public String name() {
        return "StatGateWay";
    }
}
